package com.zmzncs.lmtc.module.shop.service.impl;

import com.zmzncs.lmtc.common.util.DateTimeUtil;
import com.zmzncs.lmtc.common.util.ExceptionUtil;
import com.zmzncs.lmtc.common.util.ShortMsgUtil;
import com.zmzncs.lmtc.module.customer.dao.CustTicketDao;
import com.zmzncs.lmtc.module.customer.dao.CustomerDao;
import com.zmzncs.lmtc.module.customer.pojo.customer.Customer;
import com.zmzncs.lmtc.module.customer.pojo.ticket.CustTicket;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import com.zmzncs.lmtc.module.park.pojo.parkinfo.ParkInfo;
import com.zmzncs.lmtc.module.shop.dao.ShopInfoMapper;
import com.zmzncs.lmtc.module.shop.pojo.shop_info.ShopInfoDTO;
import com.zmzncs.lmtc.module.shop.pojo.shop_ticket.ShopTicket;
import com.zmzncs.lmtc.module.shop.dao.ShopTicketMapper;
import com.zmzncs.lmtc.module.shop.pojo.shop_ticket.ShopTicketDTO;
import com.zmzncs.lmtc.module.shop.service.ShopTicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 商户-优惠券 服务实现类
 * </p>
 *
 * @author 高博文
 * @since 2021-05-20
 */
@Service
public class ShopTicketServiceImpl extends ServiceImpl<ShopTicketMapper, ShopTicket> implements ShopTicketService {

    @Autowired
    ShopTicketMapper shopTicketMapper;
    @Autowired
    CustomerDao customerDao;
    @Autowired
    ParkInfoDao parkInfoDao;
    @Autowired
    ShopInfoMapper shopInfoMapper;
    @Autowired
    CustTicketDao custTicketDao;

    /**
     * 发送商户优惠券
     */
    @Override
    @Async("threadPoolExecutor")
    public void sendShopTicket(Long customerId,Long parkId) {
        Customer customer = customerDao.selectById(customerId);
        if (customer == null){
            log.error("发放商户优惠券，用户不存在");
            ExceptionUtil.businessException("发放商户优惠券，用户不存在");
        }
        if (customer.getMobilePhone() == null){
            log.error("发放商户优惠券，用户没有授权手机号");
            ExceptionUtil.businessException("发放商户优惠券，用户没有授权手机号");
        }
        ParkInfo parkInfo = parkInfoDao.selectById(parkId);
        if (parkInfo == null){
            log.error("发放商户优惠券，停车场不存在");
            ExceptionUtil.businessException("发放商户优惠券，停车场不存在");
        }

        //查询停车场周围商户优惠券信息
        List<ShopTicketDTO> ticketList = shopTicketMapper.getNearShopTicket(parkInfo.getLongitude(), parkInfo.getLatitude());
        if (ticketList != null && ticketList.size() > 0){

            for (ShopTicketDTO shopTicket : ticketList) {
                CustTicket custTicket = new CustTicket();
                custTicket.setTicketId(shopTicket.getId());
                custTicket.setTicketName(shopTicket.getTicketName());
                custTicket.setCustomerId(customer.getId());
                custTicket.setUseLimitMoney(shopTicket.getLimitQuantity());
                custTicket.setTipsStatus(1);
                custTicket.setPreferentialType(shopTicket.getPreferentialType());
                custTicket.setPreferentialMoney(shopTicket.getOffsetMoney());
                custTicket.setDiscountRate(shopTicket.getDiscountRate());
                custTicket.setUseType(10);
                custTicket.setStatus(0);
                custTicket.setGrantType(1);
                custTicket.setExpiryDays(shopTicket.getExpiryDays());
                custTicket.setReceiveTime(new Date());
                custTicket.setUseBeginTime(new Date());
                custTicket.setUseEndTime(DateTimeUtil.getCurrNextDayDate(shopTicket.getExpiryDays()));
                custTicketDao.insert(custTicket);
            }


            //给用户发送短信通知
            List<String>  shopNameList =  ticketList.stream().map(ShopTicketDTO::getShopName).collect(Collectors.toList());
            String shopName = String.join(",", shopNameList);

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("shop", shopName);
            //发送短信通知
            Map sendMap = ShortMsgUtil.sendNotice("SMS_218163172", customer.getMobilePhone(), paramMap);
            if(sendMap.get("Code").equals("OK")){
                log.error("短信通知发送成功");
            } else {
                log.error("短信通知发送失败");
                ExceptionUtil.businessException("短信通知发送失败");
            }
        }



    }
}
